package com.awenhui.demo.core.utils.poi;

//import com.aspose.words.Document;
//import com.aspose.words.License;
//import com.aspose.words.SaveFormat;
import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuxf1
 * @version 1.0
 * @date 2020/7/24 14:33
 */
public class WordUtils {

    /**
     * 生成word
     * @param tempatePath word模板路径
     * @param targetPath word路径
     * @param params 模板参数及值
     * @throws IOException
     */
    public static void createWord(String tempatePath, String targetPath, Map<String,String> params) throws IOException {
        //从模板读取HWPFDocument
        HWPFDocument doc = new HWPFDocument(new FileInputStream(tempatePath));
        //替换读取到word模板的指定字段
        Range range = doc.getRange();
        for (Map.Entry<String,String> entry:params.entrySet()) {
            range.replaceText(entry.getKey(),entry.getValue());
        }
        //替换word模板的页头页脚
        Range headerStoryRange = doc.getHeaderStoryRange();
        for (Map.Entry<String,String> entry:params.entrySet()) {
            headerStoryRange.replaceText(entry.getKey(),entry.getValue());
        }
        //生成word文件
        FileOutputStream fileOutputStream=new FileOutputStream(targetPath);
        doc.write(fileOutputStream);
        doc.close();
        fileOutputStream.close();
    }



//    public  static  void getLicense() throws Exception {
//        InputStream resourceAsStream = WordUtils.class.getClassLoader().getResourceAsStream("\\license.xml");
//        License license=new License();
//        license.setLicense(resourceAsStream);
//    }

//    /**
//     * word转换为pdf
//     * @param sFileName
//     * @param toFileName
//     */
//    public static void wordToPdf(String sFileName,String toFileName) throws Exception {
//        Document doc = new Document(sFileName);// 原始word路径
//        File pdfFile = new File(toFileName);// 输出路径
//        FileOutputStream fileOS = new FileOutputStream(pdfFile);
//        doc.save(fileOS, SaveFormat.PDF);
//    }

    /**
     * word占位用{{object}}比较完美可以填充图片
     * @param filePath
     * @param params
     * @param outFilePath
     * @return
     * @throws Exception
     */
    public static String tl(String filePath,String outFilePath, Map<String, Object> params)throws Exception {
        XWPFTemplate template = XWPFTemplate.compile(filePath).render(params);
        FileOutputStream out = new FileOutputStream(outFilePath);
        template.write(out);
        out.flush();
        out.close();
        template.close();
        return "";
    }

        public static void main(String[] args) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("title","大字报");
        map.put("name","惠惠");
        map.put("cno","2020091725362");
        tl("D:\\wordtest\\1.docx","D:\\wordtest\\2.docx",map);
          //  wordToPdf("D:\\wordtest\\2.docx","D:\\wordtest\\2.pdf");
        System.out.println("成功");
    }
}
